package io.dts.datasource.wrapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import io.dts.common.context.DtsContext;
import io.dts.datasource.DtsConnection;
import io.dts.datasource.wrapper.executor.StatementExecutor;

/**
 * Created by guoyubo on 2017/9/20.
 */
public class DtsStatementWrapper extends AbstractDtsStatement {


  public DtsStatementWrapper(final DtsConnection dtsConnection, final Statement statement) {
    super(dtsConnection, statement);
  }

  @Override
  public ResultSet executeQuery(final String sql) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).executeQuery();
    } catch (Exception e) {
      throw new SQLException(e);
    }

  }

  @Override
  public int executeUpdate(final String sql) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).executeUpdate();
    } catch (Exception e) {
      throw new SQLException(e);
    }

  }

  @Override
  public void close() throws SQLException {
    getRawStatement().close();
  }


  @Override
  public boolean execute(final String sql) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).execute();
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }

  @Override
  public void addBatch(final String sql) throws SQLException {
    setTargetSql(sql);
    if (DtsContext.getInstance().inTxcTransaction()) {
      throw new UnsupportedOperationException("unsupport add batch in dts transaction");
    }
    getRawStatement().addBatch(sql);
  }

  @Override
  public void clearBatch() throws SQLException {
    if (DtsContext.getInstance().inTxcTransaction()) {
      throw new UnsupportedOperationException("unsupport add batch in dts transaction");
    }
    getRawStatement().clearBatch();
  }

  @Override
  public int[] executeBatch() throws SQLException {
    if (DtsContext.getInstance().inTxcTransaction()) {
      throw new UnsupportedOperationException("unsupport add batch in dts transaction");
    }
    return getRawStatement().executeBatch();
  }

  @Override
  public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).executeUpdate(autoGeneratedKeys);
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }

  @Override
  public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).executeUpdate(columnIndexes);
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }

  @Override
  public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).executeUpdate(columnNames);
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }

  @Override
  public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).execute(autoGeneratedKeys);
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }

  @Override
  public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).execute(columnIndexes);
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }

  @Override
  public boolean execute(final String sql, final String[] columnNames) throws SQLException {
    try {
      setTargetSql(sql);
      return new StatementExecutor(createStatementModel(sql)).execute(columnNames);
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }


}
